const app = getApp()
Page({
    data: {
        hide_dialog: true, //隐藏输入弹窗
        toast_value: "", //弹窗的值每次关闭设置为空，再次出现不会有内容
        input_value: "", //输入监听持续复制，完成输入后使用
        dialog_type: "", //弹窗作用类型，判断触发者
        dialog_type_value: "", //判断类型的传值
        position: [], //楼层座位集合
        hasSeat: false, //刷新后判断没座位就显示空
        dialog_hint: "请输入内容",
        nowPosttion: "", //当前选择的楼层，用于增删显示该楼层座位，每次刷新显示一楼或最前位置的座位
        nowPosition_id: "",
        seats: []
    },
    refresh() { //每次拉取座位就吧第一个座位的所有座位信息拉去出来
        wx.cloud.callFunction({
            name: 'function',
            data: {
                type: "getPosition"
            }
        }).then(res => {
            if (res.result.data.length > 0) {
                this.setData({
                    position: res.result.data,
                    hasSeat: true,
                    nowPosttion: res.result.data[0].position,
                    nowPosition_id: res.result.data[0]._id
                })
                wx.cloud.callFunction({
                    name: 'function',
                    data: {
                        type: "getSeat",
                        position_id: this.data.nowPosition_id
                    }
                }).then(res => {
                    if (res.result.data.length > 0) {
                        this.setData({
                            seats: res.result.data,
                            hasSeat: true
                        })
                    } else {
                        this.setData({
                            hasSeat: false
                        })
                    }

                })
            } else {
                this.setData({
                    hasSeat: false,
                    position: [],
                    nowPosttion: "",
                    nowPosition_id: ""
                })
            }
        })
    },
    onLoad: function (options) {
        this.refresh()
    },
    add_seat() {
        if (this.data.nowPosttion != null && this.data.nowPosttion != "") {
            this.setData({
                hide_dialog: false,
                dialog_type: "add_seat",
                dialog_hint: "请输入座位号或名字"
            })
        } else {
            wx.showModal({
                title: '提示',
                confirmText: "知道了",
                showCancel: false,
                content: "请先添加位置"
            })
        }
    },
    tab_clisk(e) {
        this.setData({
            nowPosttion: this.data.position[e.target.dataset.idx].position,
            nowPosition_id: this.data.position[e.target.dataset.idx]._id,
        })
        wx.cloud.callFunction({
            name: 'function',
            data: {
                type: "getSeat",
                position_id: this.data.nowPosition_id
            }
        }).then(res => {
            if (res.result.data.length > 0) {
                this.setData({
                    seats: res.result.data,
                    hasSeat: true
                })
            } else {
                this.setData({
                    hasSeat: false
                })
            }
        })
    },
    changeInput(e) {
        this.setData({
            input_value: e.detail.value
        })
    },
    dialogCancel() {
        this.setData({
            hide_dialog: true,
            toast_value: "",
            dialog_type: ""
        })
    },
    dialogConfirm(e) {
        this.setData({
            hide_dialog: true,
            toast_value: ""
        })
        /**
         * dialog_type来判断input_value
         */
        switch (this.data.dialog_type) {
            case "add_position":
                wx.cloud.callFunction({
                    name: 'function',
                    data: {
                        type: "addPosition",
                        position: this.data.input_value
                    }
                }).then(res => {
                    this.refresh()
                })
                break
            case "add_seat":
                wx.cloud.callFunction({
                    name: 'function',
                    data: {
                        type: "add_seat",
                        seat: this.data.input_value,
                        position_id: this.data.nowPosition_id
                    }
                }).then(res => {
                    wx.cloud.callFunction({
                        name: 'function',
                        data: {
                            type: "getSeat",
                            position_id: this.data.nowPosition_id
                        }
                    }).then(res => {
                        this.setData({
                            seats: res.result.data,
                            hasSeat: true
                        })
                    })
                })
                break
            case "changeSeat":
                console.log(this.data.dialog_type_value)
                console.log(this.data.input_value)
                wx.cloud.callFunction({
                    name: 'function',
                    data: {
                        type: "changeSeat",
                        _id: this.data.dialog_type_value,
                        data: {
                            seat: this.data.input_value,
                        }
                    }
                }).then(res => {
                    this.refreshSeat()
                })
                break
        }
    },
    refreshSeat() {
        wx.cloud.callFunction({
            name: 'function',
            data: {
                type: "getSeat",
                position_id: this.data.nowPosition_id
            }
        }).then(res => {
            if (res.result.data.length > 0) {
                this.setData({
                    seats: res.result.data,
                    hasSeat: true
                })
            } else {
                this.setData({
                    hasSeat: false
                })
            }
        })
    },
    seatTap(e) {
        this.setData({
            dialog_type_value: e.currentTarget.dataset.item._id
        })
        var that = this
        wx.showActionSheet({
            itemList: ["查看详情", "修改名字", "删除"],
            success: function (res) {
                if (!res.cancel) {
                    switch (res.tapIndex) {
                        case 2:
                            wx.cloud.callFunction({
                                name: 'function',
                                data: {
                                    type: "remove_seat",
                                    _id: e.currentTarget.dataset.item._id
                                }
                            }).then(res => {
                                that.refreshSeat()
                            })
                            break
                        case 0:

                            break
                        case 1:
                            that.setData({
                                hide_dialog: false,
                                dialog_type: "changeSeat",
                                dialog_hint: "请输入座位号或名字"
                            })
                            break
                    }
                }
            }
        })
    },
    add_position() {
        this.setData({
            hide_dialog: false,
            dialog_type: "add_position",
            dialog_hint: "请输入位置或楼层"
        })
    },
    removePosition() {
        var that = this
        wx.showModal({
            title: '提示',
            content: '真的要删除吗',
            success: function (res) {
                if (res.confirm) {
                    wx.cloud.callFunction({
                        name: 'function',
                        data: {
                            type: "remove_position",
                            _id: that.data.nowPosition_id
                        }
                    }).then(res => {
                        that.refresh()
                    })
                }
            }
        })
    },
    onShareAppMessage: function () {}
})